package org.example.spring.controller.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.spring.context.BaseContext;
import org.example.spring.domain.dto.OrderDTO;
import org.example.spring.domain.pojo.Order;
import org.example.spring.domain.pojo.Result;
import org.example.spring.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Map;

@RequestMapping("/user/order")
@RestController("userOrderController")
@Api(tags = "订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("添加订单")
    @RequestMapping("/addOrder")
    public Result<Integer> addOrder(@RequestBody Map<String, Object> orderData) {
        double totalAmount = ((Number) orderData.get("totalAmount")).doubleValue();
        Order order = new Order();
        order.setTotalAmount(totalAmount);
        order.setStatus("0");
        order.setUserId(BaseContext.getCurrentId());
        order.setOrderDate(LocalDateTime.now());
        Integer orderId= orderService.addOrder(order);
        return Result.success(orderId);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/getOrdersByPage")
    public Result getOrdersByPage(OrderDTO orderDTO) {
        Integer userId = BaseContext.getCurrentId();
        System.out.println("当前用户id为"+userId);
        OrderDTO od= orderService.getOrdersByPage(orderDTO);
        return Result.success(od);
    }
}
